if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetQuestionnaireAnswers]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetQuestionnaireAnswers]
GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO    
/******************************************************************  
* Name:   GetQuestionnaireAnswers  
* Purpose:  Retrieves a list of QuestionnaireAnswer based on the ProtocolVersions's  
*     id passed in as a parameter.  
*  
* PARAMETERS  
* Name      Description       
* -------------   -------------------------------------------  
* @protocolVersionId The Id of the ProtocolVersion whose   
*   QuestionnaireAnswers are to be retrieved.  
******************************************************************/  
  
CREATE Procedure [dbo].[GetQuestionnaireAnswers] (  
  @protocolVersionId  INTEGER   
)  
AS  
BEGIN  
  
 SET NOCOUNT ON  
  
 Select   QA.Id,  
     QA.QuestionnaireVersionId,   
     QA.Name,  
     QA.Required,  
     QA.Editable,  
     Q.Category as 'QuestionnaireCategory',  
     QA.LastModifiedDate,  
     QA.ExternalId,  
     QA.IsClone,  
     QA.ActionTypeId,  
     QA.Highlight,
	 QA.LastmodifiedByAdmPersonId,
	 Adp.LastName+', '+ Adp.FirstName as LastModifiedBy
    FROM QuestionnaireAnswer QA  
     INNER JOIN QuestionnaireVersion QV   
      on QA.QuestionnaireVersionId = QV.ID  
     INNER JOIN Questionnaire Q  
      on QV.QuestionnaireId = Q.Id  
     INNER JOIN ProtocolVersion_QuestionnaireAnswer_Map Map  
      on QA.Id = Map.QuestionnaireAnswerId 
	 Left Join AdmPerson Adp
	  On Adp.Id = QA.LastmodifiedByAdmPersonId 
     
   WHERE Map.ProtocolVersionId = @protocolVersionId  
     
   order by QA.Id ASC  
   
END  
  
  